/*
 * Copyright (C) 2016 Nishant Srivastava
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.nisrulz.sensey;


import ohos.agp.components.Component;
import ohos.agp.components.ScaleInfo;

public class PinchScaleDetector {

    private class ScaleGestureListener implements Component.ScaledListener {

        @Override
        public void onScaleStart(Component component, ScaleInfo scaleInfo) {
            pinchScaleListener.onScaleStart(scaleInfo);
            countOfScaleOut = 0;
            countOfScaleIn = 0;
            eventOccurred = 0;
        }

        @Override
        public void onScaleUpdate(Component component, ScaleInfo scaleInfo) {
            double scaleFactor = scaleInfo.scale;
            if (scaleFactor > 1) {
                countOfScaleIn += 1;
                if (eventOccurred != 1 && countOfScaleIn > 2) {
                    eventOccurred = 1;
                    pinchScaleListener.onScale(scaleInfo, true);
                }
            } else {
                countOfScaleOut += 1;
                if (eventOccurred != 2 && countOfScaleOut > 2) {
                    eventOccurred = 2;
                    pinchScaleListener.onScale(scaleInfo, false);
                }
            }
        }

        @Override
        public void onScaleEnd(Component component, ScaleInfo scaleInfo) {
            pinchScaleListener.onScaleEnd(scaleInfo);
        }
    }

    public interface PinchScaleListener {

        void onScale(ScaleInfo scaleInfo, boolean isScalingOut);

        void onScaleEnd(ScaleInfo scaleInfo);

        void onScaleStart(ScaleInfo scaleInfo);
    }

    private int countOfScaleIn;

    private int countOfScaleOut;

    private int eventOccurred;

    private final PinchScaleListener pinchScaleListener;

    private final ScaleGestureListener scaleGestureDetector;
    private Component component;

    public PinchScaleDetector(Component component, PinchScaleListener pinchScaleListener) {
        this.component = component;
        scaleGestureDetector = new ScaleGestureListener();
        this.pinchScaleListener = pinchScaleListener;
        this.eventOccurred = 0;
        this.countOfScaleIn = 0;
        this.countOfScaleOut = 0;
        this.component.setScaledListener(scaleGestureDetector);
    }

    public void setScaledListener(Component component) {
        component.setScaledListener(scaleGestureDetector);
    }

    public void unregisterListener() {
        component.setScaledListener(null);
    }
}
